package com.taichuan.selfcheck.ui.fragment;

import android.app.Service;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taichuan.keysupport.Key;
import com.taichuan.keysupport.view.panel.T12KSPanelEditText;
import com.taichuan.selfcheck.BaseApplication;
import com.taichuan.selfcheck.R;
import com.taichuan.selfcheck.ad.ADView;
import com.taichuan.selfcheck.config.PropertyDefaultValue;
import com.taichuan.selfcheck.eventbus.DownProgressEvent;
import com.taichuan.selfcheck.mvp.presenter.VideoPresenter;
import com.taichuan.selfcheck.mvp.viewinterface.VideoTestInterface;
import com.taichuan.selfcheck.ui.dialog.MessageDialog;
import com.taichuan.selfcheck.ui.dialog.ResultDialog;
import com.taichuan.selfcheck.ui.fragment.base.AppBaseFragment;
import com.taichuan.selfcheck.utils.ADViewPersistent;
import com.taichuan.tcutils.taichuan.SystemUtils;
import com.taichuan.tcutils.taichuan.TcDeviceType;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 该类主要用于
 *
 * @author CamelLuo
 * @version 2019/12/5
 */
public class VideoTestFragment extends AppBaseFragment<VideoTestInterface, VideoPresenter> implements VideoTestInterface {

    @BindView(R.id.lly_main_ads)
    RelativeLayout mADLayout;
    @BindView(R.id.adUrlPath)
    T12KSPanelEditText adUrlPath;
    @BindView(R.id.ad_progress_tv)
    TextView ad_progress_tv;

    private RelativeLayout.LayoutParams mLayoutMatchParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    private ADView mAdView;
    //测试结果对话框
    ResultDialog resultDialog;
    //用于更改媒体音量
    AudioManager audio;
    private MessageDialog dialog;
    private String curProgress;

    @Override
    protected void getBundleValues(Bundle bundle) {

    }

    @Override
    protected int setContentViewId() {
        return R.layout.fragment_ad_test;
    }

    @Override
    protected void initView(View view, Bundle bundle) {
        mAdView = ADViewPersistent.get().getTestADView(getActivity());
        mADLayout.addView(mAdView, mLayoutMatchParams);
        resultDialog = new ResultDialog(PropertyDefaultValue.FUNCTION_TEST_13,getResources().getString(R.string.video_test), mActivity);       //设置弹窗发送的事件——视频测试
        dialog=new MessageDialog(mActivity);        //适配按键版机型，调整初始化的位置

        audio = (AudioManager) getActivity().getSystemService(Service.AUDIO_SERVICE);
        EventBus.getDefault().register(this);

//        if(SystemUtils.getTcDeviceType() == TcDeviceType.A64_LANDSCAPE_KEY){
//            mViewFocus.add(adUrlPath);
//            adUrlPath.init(mActivity,false);
//        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mAdView.resume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mAdView.pause();
        mADLayout.removeView(mAdView);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    protected VideoPresenter createPresenter() {
        return new VideoPresenter();
    }

    @OnClick({R.id.adDownLoad,R.id.adLoadSDCard})
    @Override
    protected void onXmlClick(View v) {
        super.onXmlClick(v);
        switch (v.getId()) {
            case R.id.adDownLoad: {
                String videoUrl=adUrlPath.getText().toString().trim();
                mPresenter.downLoadVideo(videoUrl,getActivity(),mAdView.isPlayAD());
                break;
            }
            case R.id.adLoadSDCard:{
                mPresenter.copyVideoFile();
                break;
            }
        }
    }

    @Override
    protected boolean isAllowAutoFinishFragment() {
        return false;
    }

    @Override
    public void doOnAutoFinishFragment() {
        resultDialog.show();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (resultDialog.isShowing()) {
            resultDialog.dispatchKeyEvent(event);
        }
        if (dialog.isShowing()){
            dialog.dispatchKeyEvent(event);
        }
//        if(mViewFocus.onFocused(adUrlPath)){
//            return super.dispatchKeyEvent(event);
//        }
        if(event.getKeyCode()== Key.Convert.KEYCODE_LONG_XING){
            String videoUrl=adUrlPath.getText().toString().trim();
            mPresenter.downLoadVideo(videoUrl,getActivity(),mAdView.isPlayAD());
        }else if(event.getKeyCode() == Key.Convert.KEYCODE_JING){
            mPresenter.copyVideoFile();
        }else if(event.getKeyCode() == Key.Convert.KEYCODE_1){
            if(dialog != null && dialog.isShowing()){
                dialog.dismiss();
                mPresenter.toDownVideo(adUrlPath.getText().toString().trim());
            }
        }else if(event.getKeyCode() == Key.Convert.KEYCODE_3){
            if(dialog != null && dialog.isShowing()){
                dialog.dismiss();
            }
        }
        boolean result = mPresenter.changeVolume(audio,event.getKeyCode());
        return result ? result : super.dispatchKeyEvent(event);
    }

    @Override
    public void pauseVideoPlay() {
        mAdView.pause();
    }

    @Override
    public void playVideo() {
        mAdView.resume();
    }

    @Override
    public void showMessageDialog(String videoUrl) {

        dialog.setNormalOnClickListener(new MessageDialog.NormalOnClickListener() {
            @Override
            public void onNormalClick() {
                dialog.dismiss();
                mPresenter.toDownVideo(videoUrl);
            }
        });
        dialog.setErrorOnClickListener(new MessageDialog.ErrorOnClickListener() {
            @Override
            public void onErrorClick() {
                dialog.dismiss();
            }
        });
        dialog.show();
    }


    @Override
    public void showLoading() {
        mActivity.showStaticDialog("加载中，请稍后...");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(DownProgressEvent event){
        if(event.getProgress()==100){
            ad_progress_tv.setVisibility(View.GONE);
        }else{
            ad_progress_tv.setVisibility(View.VISIBLE);
            curProgress="下载进度: "+event.getProgress()+"%";
            ad_progress_tv.setText(curProgress);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
